package com.ews.onlineexchange.controller;

import java.util.Arrays;

import javax.enterprise.context.ConversationScoped;
import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.EntityManager;

import com.ews.onlineexchange.model.DataEntity;
import com.ews.onlineexchange.model.ProfessionUser;
import com.ews.onlineexchange.model.User;
import com.ews.onlineexchange.service.EntityHome;
import com.ews.onlineexchange.service.UserSeqNoGenerator;
import com.ews.onlineexchange.service.UserService;
import com.ews.onlineexchange.util.AesUtil;

@Named
@ConversationScoped
public class ProfessionUserHome extends EntityHome<ProfessionUser, String> {
	
	private static final long serialVersionUID = 1L;

	
	@Inject
	UserSeqNoGenerator userSeqNoGenerator;
	
	@Inject
	EntityManager entityManager;
	@Inject
	UserService userService;
	@Override
	public String saveAndFlush() {
		getInstance().setAuditted(true);
		boolean managed = this.isManaged();
		User user = userService.getLoginedUser();
		updateOperator(user);
		if(!managed){
			getInstance().setRoles("profession");
			getInstance().setShiroPwd(userService.entryptPassword(getInstance().getPassword()));
		}
		String result = super.saveAndFlush();
//		if (!managed && RESULT_SAVED.equals(result)) {
//			keycloakUserService.persistUser(getInstance(), Arrays.asList(KeycloakUserService.ROLE_PROFESSION));
//			keycloakUserService.enableUser(getInstance());
//		} else {
//			keycloakUserService.updateRoles(getInstance(), Arrays.asList(KeycloakUserService.ROLE_PROFESSION));
//			keycloakUserService.updateEmail(getInstance());
//		}
		return "ProfessionUserList.xhtml?faces-redirect=true&amp;includeViewParams=true&amp;firstResult=0";
	}

	@Override
	public String delete() {
		User user = userService.getLoginedUser();
		updateOperator(user);
		getInstance().setDelFlag(DataEntity.DEL_FLAG_DELETE);
		String result = super.saveAndFlush();
		if (result.equals(RESULT_SAVED)) {
//			keycloakUserService.disableUser(getInstance());
			return "ProfessionUserList.xhtml?faces-redirect=true&amp;includeViewParams=true&amp;firstResult=0";
		}
		return null;
	}
	
	private String confirmPassword;

	public String getConfirmPassword() {
		return confirmPassword;
	}

	public void setConfirmPassword(String confirmPassword) {
		this.confirmPassword = confirmPassword;
	}
	
}
